
package ai.moran.demo.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.SystemUtils;
import org.omg.CORBA.SystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

public class LogUtility {
    private static final ConcurrentHashMap<String, Logger> loggerMap = new ConcurrentHashMap();

    public LogUtility() {
    }

    public static Logger addLogger(String loggerName) {
        if (StringUtils.isEmpty(loggerName)) {
            throw new IllegalArgumentException("looger name is emtpy");
        } else {
            Logger newLogger = LoggerFactory.getLogger(loggerName);
            Logger logger = (Logger)loggerMap.putIfAbsent(loggerName, newLogger);
            return logger != null ? logger : newLogger;
        }
    }

    public static Logger getLogger(String loggerName) {
        Logger logger = (Logger)loggerMap.get(loggerName);
        if (logger == null) {
            String errorInfo = String.format("no logger for name: %s, please use addLogger to register", loggerName);
            throw new IllegalArgumentException(errorInfo);
        } else {
            return logger;
        }
    }

    public static void warn(Logger logger, Exception e) {
        String format = "warn exception";
        if (e.getCause() != null) {
            format = format + ", stack trace:" + SystemUtils.LINE_SEPARATOR + getStackTraceString(e.getCause().getStackTrace());
            logger.warn(format);
        } else if (e.getStackTrace() != null) {
            format = format + ", stack trace:" + SystemUtils.LINE_SEPARATOR + getStackTraceString(e.getStackTrace());
            logger.warn(format);
        } else {
            format = format + ", stack trace:" + SystemUtils.LINE_SEPARATOR + getStackTraceString(Thread.currentThread().getStackTrace());
            logger.warn(format);
        }

    }

    public static void warn(Logger logger, String format, Object... arguments) {
        format = format + ", stack trace:" + SystemUtils.LINE_SEPARATOR + getStackTraceString(Thread.currentThread().getStackTrace());
        logger.warn(format, arguments);
    }

    public static void logInfo(Logger logger, String message) {
        logger.info(message);
    }

    public static void logInfo(Logger logger, String format, Object... arguments) {
        logger.info(format, arguments);
    }

    public static void logError(Logger logger, String format, Object... arguments) {
        format = format + ", stack trace:" + SystemUtils.LINE_SEPARATOR + getStackTraceString(Thread.currentThread().getStackTrace());
        logger.error(format, arguments);
    }

    public static String logToString(Throwable e) {

        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        ((Throwable) e).printStackTrace(printWriter);
        return stringWriter.toString();
    }

    public static void logException(Logger logger, Throwable e) {
        logger.error(logToString(e));
    }

    public static String getStackTraceString(StackTraceElement[] stackTraceElements) {
        StringBuilder stringBuilder = new StringBuilder();
        if (stackTraceElements != null) {
            StackTraceElement[] var2 = stackTraceElements;
            int var3 = stackTraceElements.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                StackTraceElement element = var2[var4];
                stringBuilder.append(element).append(SystemUtils.LINE_SEPARATOR);
            }
        }

        return stringBuilder.toString();
    }
}
